package com.dx.mes.common.transcoder.util;


import com.dx.mes.common.bean.entity.LocalInfoBean;

import java.net.InetAddress;
import java.net.UnknownHostException;


/**
* @Title: LocalUtil.java
* @Package com.util
* @Description: TODO(添加描述)
* @author Lynn.Sea
* @date 2015-2-13 上午8:25:41    
* @version V1.0   
*/
public class LocalUtil {
	private static LocalInfoBean localInfo;
	static {
		localInfo = new LocalInfoBean();
		try {
			InetAddress inetAddr = InetAddress.getLocalHost();
			byte[] addr = inetAddr.getAddress();
			// Convert to dot representation
			String ipAddr = "";
			for (int i = 0; i < addr.length; i++) {
				if (i > 0) {
					ipAddr += ".";
				}
				ipAddr += addr[i] & 0xFF;
			}
			String hostname = inetAddr.getHostName();
			if (hostname.length() > 20) {
				localInfo.setHostName(hostname.substring(0, 20));
			} else {
				localInfo.setHostName(hostname);
			}
			localInfo.setIp(ipAddr);
			// System.out.println("IP Address: " + ipAddr);
			// System.out.println("Hostname: " +hostname );
		} catch (UnknownHostException e) {
			System.out.println("Host not found: " + e.getMessage());
		}
	}
	
	
	
	/*****
	 * 获取主机名和ip
	 * ******/
	public static LocalInfoBean getLocalInfo() {
		return localInfo;
	}
}
